package com.xdjk.networkdemo.utils;


import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.xdjk.networkdemo.R;

public class DialogUtils {
    public static final int ProgressMax = 100;

    /**
     * 得到自定义的Dialog
     *
     * @param context
     * @param msg
     * @return
     */
    public static Dialog createLoadingDialog(Context context, String msg) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);
        // 加载动画
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
        // 使用ImageView显示动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);
        tipTextView.setText(msg);// 设置加载信息

        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog

        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setCanceledOnTouchOutside(false);//点击弹框外不消失
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int) (AppUtils.getScreenWidth(context) * 0.8), (int) (AppUtils.getScreenWidth(context) * 0.4));
        loadingDialog.setContentView(layout, layoutParams);// 设置布局
        return loadingDialog;

    }


    /**
     * 得到自定义的progressDialog
     *
     * @param context
     * @param msg
     * @return
     */
    public static ProgressDialog createProgressDialog(Context context,String title, String msg) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        // 设置进度条风格，风格为长形
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 设置ProgressDialog 标题
        if (!TextUtils.isEmpty(title)) {
            progressDialog.setTitle(title);
        }
        // 设置ProgressDialog 提示信息
        progressDialog.setMessage(msg);
        // 设置ProgressDialog 的进度条是否更新
        progressDialog.setIndeterminate(false);
        // 设置ProgressDialog 是否可以按退回按键取消
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMax(ProgressMax);
        return progressDialog;

    }


}
